package com.xinghou.platform.server.api.controller;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.xinghou.platform.dao.main.SuggestionDAO;
import com.xinghou.platform.dao.main.UserDAO;
import com.xinghou.platform.entity.req.SuggestionReq;
import com.xinghou.platform.model.main.Suggestion;
import com.xinghou.platform.model.main.User;
import com.xinghou.platform.model.main.VersionConfigReq;
import com.xinghou.platform.server.api.common.ResponseTools;
import com.xinghou.platform.server.api.service.VersionConfigService;
import com.xinghou.platform.util.*;
import com.xinghou.platform.util.exception.DBExceptionEnums;
import com.xinghou.platform.util.exception.ServiceException;
import com.xinghou.platform.server.api.log.ApiLogger;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import com.xinghou.platform.dao.main.VersionConfigDAO;
import com.xinghou.platform.entity.req.ServerInfoReq;
import com.xinghou.platform.entity.resp.ServerInfo;
import com.xinghou.platform.entity.resp.ServerInfoResp;
import com.xinghou.platform.model.main.VersionConfig;
import com.xinghou.platform.server.api.log.LoggerFactory;

@Controller
public class ServerInfoController extends BaseControl {
    private String key = ConfigUtil.getProperty("HTTP_SERVICE_KEY");
    private String[] iflist = ConfigUtil.getProperty("ifinfo").split(",");

    private String reqjson;//请求json
    private String repjson;//返回json

    private ServerInfoReq serverInfoReq;
    private ServerInfoResp serverInfoResp;
    private ServerInfo serverInfo;
    private List<ServerInfo> serverInfolist;
    private boolean bool = false;

    @Resource
    private VersionConfigDAO versionConfigDAO;
    @Resource
    private VersionConfigService versionConfigService;
    @Resource
    private SuggestionDAO suggestionDAO;

    /**
     * 服务端时间获取
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getServerTime", method = {RequestMethod.POST, RequestMethod.GET})
    public String getServerTime(@RequestParam(required = true) String token,
                                @RequestBody ServerInfoReq serverInfoReq,
                                HttpServletRequest request, HttpServletResponse response) {
        ServerInfoResp serverInfoResp = new ServerInfoResp();
        serverInfoResp.setServertime(DateUtils.getNewZhCNDateTime());

        return response(serverInfoResp);
    }


    @ResponseBody
    @RequestMapping(value = "/suggestion", method = {RequestMethod.POST, RequestMethod.GET})
    public String getServerTime(
                                @RequestBody SuggestionReq suggestionReq,
                                HttpServletRequest request, HttpServletResponse response) {

        Suggestion suggestion=new Suggestion();
        suggestion.setContent(suggestionReq.content);
        suggestion.setCreatetime(System.currentTimeMillis());
        suggestion.setUserid(Integer.parseInt(suggestionReq.userid));
        suggestionDAO.addSuggestion(suggestion);
        return response(new ServerInfoResp());
    }


    /**
     * 更新versionConfig
     *
     * @param versionConfig
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/versionconfigupdate", method = {RequestMethod.POST, RequestMethod.GET})
    public String versionconfigupdate(
            @RequestBody VersionConfig versionConfig
    ) {
        if (versionConfig.getOstype() == null)
            throw new ServiceException(DBExceptionEnums.UA_SYSTEM_US_NULL);
        versionConfigDAO.updateVersionConfigByOsType(versionConfig);
        return response(null);
    }

    @RequestMapping(value = "/getmsgnotify", method = {RequestMethod.POST, RequestMethod.GET})
    public void userMsgNotify(@RequestParam String yyid, @RequestParam Long count, HttpServletRequest request, HttpServletResponse response) {
        ApiLogger.log("/getmsgnotify", null);
        SignUtil.checkRequestJson(null);
        if (StringUtils.isEmpty(yyid))
            throw new ServiceException(DBExceptionEnums.YYID_IS_NULL);
        List<String> values = versionConfigService.getRangeMessNotify(yyid, count);

        HashMap map = new HashMap();
        map.put("v", values);
        String str = response(map);
        ResponseTools.get().responseResult(response, str);
    }

    @ResponseBody
    @RequestMapping(value = "/addmsgnotify", method = {RequestMethod.POST, RequestMethod.GET})
    public String addUserMsgNotify(@RequestBody VersionConfigReq versionConfigReq,HttpServletRequest request, HttpServletResponse response
                                   ) {
        if (StringUtils.isEmpty(versionConfigReq.yyid))
            throw new ServiceException(DBExceptionEnums.YYID_IS_NULL);
        if (StringUtils.isEmpty(versionConfigReq.uid))
            throw new ServiceException(DBExceptionEnums.USERID_US_NULL);
        if (StringUtils.isEmpty(versionConfigReq.nickname) || StringUtils.isEmpty(versionConfigReq.mjnickname))
            throw new ServiceException(DBExceptionEnums.NICKNAME_IS_NULL);
  /*      User user = userDAO.getUserById(Integer.parseInt(versionConfigReq.uid));
        User mjuser = userDAO.getUserById(Integer.parseInt(versionConfigReq.mjuid));
        if (user != null) versionConfigReq.nickname = user.getNickname();
        if (mjuser != null) versionConfigReq.mjnickname = mjuser.getNickname();*/
        versionConfigService.addMessageNotifyRel(versionConfigReq.uid, versionConfigReq.mjuid, versionConfigReq.nickname, versionConfigReq.mjnickname, versionConfigReq.yyid);
        return response(null);
    }


    public static void main(String args[]) {

        String a = "%E5%B0%8F%E9%B9%8F%E5%84%BF";
        String b = "";
        try {
            b = java.net.URLDecoder.decode(a, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        System.out.println(b);
    }


    /**
     * 检查app版本更新
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/checkupdate", method = {RequestMethod.POST, RequestMethod.GET})
    public String checkupdate(
            @RequestBody VersionConfigReq versionConfigReq,
            HttpServletRequest request, HttpServletResponse response) {
        if (StringUtils.isBlank(versionConfigReq.getUasystem()))
            throw new ServiceException(DBExceptionEnums.UA_SYSTEM_US_NULL);
        VersionConfig versionConfig = null;
        versionConfig = versionConfigDAO.getVersionConfigByOsType(Integer.parseInt(versionConfigReq.getUasystem()));
        repjson = JsonUtil.toJson(versionConfig);
        return response(versionConfig);
    }


    @ResponseBody
    @RequestMapping(value = "/getYyAccounts", method = {RequestMethod.POST, RequestMethod.GET})
    public String getYyAccount(
            @RequestBody VersionConfigReq versionConfigReq,
            HttpServletRequest request, HttpServletResponse response) {
        String accounts = versionConfigService.getBusinessAccounts();
        HashMap map = new HashMap();
        map.put("accounts", accounts);
        repjson = JsonUtil.toJson(map);
        return response(map);
    }

    @ResponseBody
    @RequestMapping(value = "/updateYyAccounts", method = {RequestMethod.POST, RequestMethod.GET})
    public String updateYyAccount(
            @RequestBody VersionConfigReq versionConfigReq,
            HttpServletRequest request, HttpServletResponse response) {
        versionConfigService.upateBusinessAccount(versionConfigReq.accounts);
        return response(repjson);
    }

    public String getReqjson() {
        return reqjson;
    }

    public void setReqjson(String reqjson) {
        this.reqjson = reqjson;
    }

    public String getRepjson() {
        return repjson;
    }

    public void setRepjson(String repjson) {
        this.repjson = repjson;
    }

    public ServerInfoReq getServerInfoReq() {
        return serverInfoReq;
    }

    public void setServerInfoReq(ServerInfoReq serverInfoReq) {
        this.serverInfoReq = serverInfoReq;
    }

    public ServerInfoResp getServerInfoResp() {
        return serverInfoResp;
    }

    public void setServerInfoResp(ServerInfoResp serverInfoResp) {
        this.serverInfoResp = serverInfoResp;
    }

    public ServerInfo getServerInfo() {
        return serverInfo;
    }

    public void setServerInfo(ServerInfo serverInfo) {
        this.serverInfo = serverInfo;
    }

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    public String[] getIflist() {
        return iflist;
    }

    public void setIflist(String[] iflist) {
        this.iflist = iflist;
    }


    public List<ServerInfo> getServerInfolist() {
        return serverInfolist;
    }

    public void setServerInfolist(List<ServerInfo> serverInfolist) {
        this.serverInfolist = serverInfolist;
    }

}
